package com.moxiao.yfjh.module.blind.controller.app.controller.sessionrecord;

import com.alibaba.fastjson.JSONObject;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.module.blind.common.GenNoCommonMethod;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordUpdateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.sessionRecord.SessionRecordDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.blind.service.memberMessage.MemberMessageService;
import com.moxiao.yfjh.module.blind.service.sessionRecord.SessionRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.module.blind.common.ErrorCodeConstants.SESSION_RECORD_CREATE_FOUNDER;
import static com.moxiao.yfjh.module.blind.common.ErrorCodeConstants.SESSION_RECORD_CREATE_PARTICIPANTS;


@Tag(name = "app - 相亲会话记录")
@RestController
@RequestMapping("/blind/session-record")
@Validated
public class AppSessionRecordController {

    @Resource
    private SessionRecordService sessionRecordService;
    @Resource
    private GenNoCommonMethod genNoCommonMethod;

    @Resource
    private MemberMessageService memberMessageService;

    @Resource
    private BlindMemberUserService memberUserService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲会话记录")
    public CommonResult<JSONObject> createSessionRecord(@Valid @RequestBody SessionRecordCreateReqVO createReqVO) {

        if (StringUtils.isEmpty(createReqVO.getFounder()) || Objects.equals(createReqVO.getFounder(),"undefined")) {
            throw exception(SESSION_RECORD_CREATE_FOUNDER);
        }

        if (StringUtils.isEmpty(createReqVO.getParticipants()) || Objects.equals(createReqVO.getParticipants(),"undefined")) {
            throw exception(SESSION_RECORD_CREATE_PARTICIPANTS);
        }

        createReqVO.setConversationId(createReqVO.getConversationID());
        JSONObject jsonObject = new JSONObject();

//        判断会话是否已经创建过
        List<SessionRecordDO> sessionRecordList = sessionRecordService.getSessionRecordList(new SessionRecordExportReqVO().setFounder(createReqVO.getFounder()).setParticipants(createReqVO.getParticipants()));
        if (CollectionUtils.isNotEmpty(sessionRecordList)) {
//            不创建会话
            jsonObject.put("userId", sessionRecordList.get(0).getFounder());
            jsonObject.put("recordID", sessionRecordList.get(0).getRecordId());
            return success(jsonObject);
        }

        List<SessionRecordDO> sessionRecordList1 = sessionRecordService.getSessionRecordList(new SessionRecordExportReqVO().setParticipants(createReqVO.getFounder()).setFounder(createReqVO.getParticipants()));
        if (CollectionUtils.isNotEmpty(sessionRecordList1)) {
//            不创建会话
            jsonObject.put("userId", sessionRecordList1.get(0).getFounder());
            jsonObject.put("recordID", sessionRecordList1.get(0).getRecordId());
            return success(jsonObject);
        }
//        没有，则创建
        genNoCommonMethod.addSessionNo(createReqVO);
        Long sessionRecord = sessionRecordService.createSessionRecord(createReqVO);
        String recordID = generateRecordId(sessionRecord, createReqVO);
        jsonObject.put("userId", createReqVO.getFounder());
        jsonObject.put("recordID", recordID);
        jsonObject.put("id", sessionRecord);
        return success(jsonObject);
    }

    public String generateRecordId(Long sessionRecordId, SessionRecordCreateReqVO createReqVO) {
        StringBuilder recordId = new StringBuilder();
        long timestamp = System.currentTimeMillis();
        recordId.append(timestamp);
        recordId.append(createReqVO.getFounder());
        recordId.append(createReqVO.getParticipants());
        recordId.append(sessionRecordId);
        SessionRecordUpdateReqVO updateReqVO = new SessionRecordUpdateReqVO();
        updateReqVO.setId(sessionRecordId);
        updateReqVO.setRecordId(recordId.toString());
        sessionRecordService.updateSessionRecord(updateReqVO);
        return recordId.toString();
    }

}
